<?php 
class GoodsController extends Controller{
	// Goods模型对象
	public $db;

	public function __init(){
		$this->db=K('Goods');
	}

	//商品列表
	public function index(){
		$goods=$this->db->getList();
		$this->assign('goods',$goods);
		$this->display();
	}

	//商品添加
	public function add(){
		if(IS_POST){			
			if($this->db->addGoods()){
				$this->success('商品添加成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			//分配栏目数据，供添加商品时选择商品所属栏目
			$this->assign('category',S('category'));
			//分配品牌数据
			$this->assign('brand',S('brand'));
			//分配商品类型数据
			$this->assign('goods_type',S('goods_type'));
			$this->display();
		}
	}

	//商品编辑
	public function edit(){
		if(IS_POST){
			if($this->db->editGoods()){
				$this->success('编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$gid=Q('gid',0,'intval');
			if(!$gid){
				$this->error('商品不存在');
			}
			//分配栏目数据
			$this->assign('category',S('category'));
			//分配品牌数据
			$this->assign('brand',S('brand'));
			//分配类型数据
			$this->assign('goods_type',S('goods_type'));
			//商品数据
			$field=K('GoodsView')->getOne($gid);
			$this->assign('field',$field);
			//相册图片数据
			$gallery=K('GoodsGallery')->getImage($gid);
			// p($gallery);
			$this->assign('gallery',$gallery);

			$this->display();
		}
	}

	//商品删除
	public function del(){
		$gid=Q('gid',0,'intval');
		if($this->db->delGoods()){			
			$this->success('商品删除成功','index');
		}else{
			$this->error($this->db->error);
		}
	}

	//获得商品属性
	public function getGoodsAttr(){
		$db=K('GoodsAttr');
		$data=$db->get();
		$this->assign('data',$data);
		$this->display();
	}

	//删除相册图片
	public function delGalleryImage(){
		$id=Q('goods_gallery_id');
		if($gallery=K('GoodsGallery')->delImage($id)){
			$this->success('相册图片删除成功');
		}else{
			$this->error('相册图片删除成功');
		}
	}

}

?>